const {User} = require('../model/index')
const {createToken} = require('../utils/jwt')
// 用户注册
module.exports.register = async (req, res) => {
    const userModel = new User(req.body)
    const dbBack = await userModel.save()
    const user = dbBack.toJSON()
    delete user.password
    res.status(200).json({
        user, msg: '注册成功'
    })
}

// 用户登录
module.exports.login = async (req, res) => {
    // 登录逻辑
    let dbBack = await User.findOne(req.body)
    if (!dbBack) {
        res.status(400).json({
            error: '邮箱或者密码不正确'
        })
    }
    dbBack = dbBack.toJSON()
    dbBack.token = await createToken(dbBack)
    res.status(200).json(dbBack)
}

// 用户列表
module.exports.list = async (req, res) => {
    res.send('user list')
}
